package com.flashtones
{

import flash.events.SampleDataEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
	
public class SoundGenerator
{
    private var sound:Sound = new Sound();
    private var spectrum:Number = 0;
    private var channel:SoundChannel;
    private var noise:Number = 0;
		
    public function SoundGenerator()
    {
        sound.addEventListener("sampleData", sampleEventHandler);
    }
		
    /*  PUBLIC METHODS  */
		
    public function play():void
    {
        channel = sound.play();
    }
		
    public function set volume(val:Number):void
    {
        var transform:SoundTransform = channel.soundTransform;
        transform.volume = val;
        channel.soundTransform = transform;
        //trace(val);
    }
		
    public function set pan(val:Number):void
    {
        var transform:SoundTransform = channel.soundTransform;
        transform.pan = val;
        channel.soundTransform = transform;
    }
		 
    public function set frequency(val:Number):void
    {
        spectrum = val;
    }
		
    /*  PRIVATE METHODS  */
		
    private function sampleEventHandler(event:SampleDataEvent):void
    {
        for(var i:uint = 0; i < 2048; i++)
			{
				noise += ((spectrum) / 44100);
				var sample:Number = noise * Math.PI * 2;
				event.data.writeFloat(Math.sin(sample));
				event.data.writeFloat(Math.sin(sample));
				event.data.writeFloat(Math.sin(sample));
				event.data.writeFloat(Math.sin(sample));
			}
    }
}
}
